<template>
	<view class="login margin-top" v-if="loadStatus">
		<u--form labelPosition="left">
			<u-form-item label="帐户名称" borderBottom :labelWidth="100">
				<u--input v-model="detail.name"></u--input>
			</u-form-item>
			<u-form-item label="帐户余额" borderBottom :labelWidth="100">
				<u--input v-model="detail.balance"></u--input>
			</u-form-item>
			<u-form-item label="帐户类型" borderBottom :labelWidth="100">
				<picker @change="account_change" :value="index" :range="accountList" range-key="name" mode="selector">
					<view class="picker">
						{{index >= 0 ? accountList[index]['name']: '请选择'}}
					</view>
				</picker>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			
			<button type="success" class="bg-theme submit" @tap="submit" :disabled="disabled">提交</button>
		</u--form>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';
	import accountModel from '../../models/AccountModel';
	var that;
	export default {
		data() {
			return {
				loadStatus: false,
				accountList: [],
				index: -1,
				detail: {
					id: '',
					name: '',
					balance: 0,
					account_type_id: ''
				},
				from: '',
				disabled: false
			}
		},
		async onLoad(options) {

			console.log(options);
			that = this;
			that.from = options.from ? options.from : '';
			if (options.detail) {
				this.detail = JSON.parse(decodeURIComponent(options.detail))
				uni.setNavigationBarTitle({
					title: '编辑账户'
				})
			}


			uni.showLoading();
			await that.get_account_type()
			that.loadStatus = true;
			uni.hideLoading();


		},
		methods: {
			...mapMutations(['changeUserInfo']),
			async get_account_type() {
				const res = await accountModel.accountTypeList()
				if (res.code == 200) {
					that.accountList = res.data;
					if (that.detail.account_type_id) {
						res.data.forEach((item, index) => {
							if (item.id == that.detail.account_type_id) {
								that.index = index;
							}
						})
					}
				}
			},
			//选择类型
			account_change(e) {
				let index = e.detail.value;
				this.index = index;
				this.detail.account_type_id = this.accountList[index]['id'];
			},
			submit() {
				var data = this.detail;
				if (!data.name) {
					this.$base.alert('请输入帐户名称');
					return false;
				}

				let that = this;

				that.disabled = true;
				uni.showLoading({
					mask: true
				})

				accountModel.add(data).then(res => {
					if (res.code == 200) {
						this.$base.getFundsType()
						uni.showToast({
							title: res.msg,
							success() {
								setTimeout(() => {
									if (that.from == 'funds') {
										uni.redirectTo({
											url: '/pages/index/add'
										})
									} else {
										var pages = getCurrentPages();
										var page = pages[pages.length - 2];
										if (page.route == 'pages/account/index') {
											page.$vm.get_account_list();
										}
										uni.navigateBack();
									}
								}, 500)
							}
						})
					} else {
						that.disabled = false;
						that.$base.alert(res.msg);
						uni.hideLoading()
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.login {
		padding: 40upx;
		
		.submit{
			margin-top: 40rpx;
		}
	}
</style>